package eu.boss.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import eu.boss.model.PlayersList;

/**
 * This class cares about http requests
 * @author Arnaud
 *
 */
public class Http {

	/**
	 * 
	 * @param url
	 * @return PlayerList send by the server
	 */
	public PlayersList executeGetRequest(String url)
			throws JsonParseException, JsonMappingException,
			IllegalStateException, IOException {

		PlayersList list = null;
		ObjectMapper jacksonMapper = new ObjectMapper();
		HttpClient client = new DefaultHttpClient();
		HttpGet get = new HttpGet(url);
		/**
		 * Adding of Accept header,
		 */
		get.addHeader("Accept", "text/plain");
		get.addHeader("Accept-Charset", "utf-8");

		HttpResponse response = client.execute(get);
		InputStream is=response.getEntity().getContent();
		String message = convertStreamToString(is);
		//ignoring "\n" character
		if (message.substring(message.length()-1).compareTo("\n")==0)
			message= message.substring(0, message.length()-1);
		list = jacksonMapper.readValue(message,
				PlayersList.class);

		return list;
	}
	
	/**
	 * Convert a Stream into a String
	 * @param is InputStream to convert
	 * @return
	 */
	
	public String convertStreamToString(InputStream is) {

		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();

		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append((line + "\n"));
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}
	
	/**
	 * 
	 * @param url
	 * @param pairs
	 * @return message response from the server (SUCCESS or FAILED) Doesn't work
	 */
	
	public String executePutRequest(String url,
			List<NameValuePair> pairs) {

		String message = null;
		ObjectMapper jacksonMapper = new ObjectMapper();
		HttpPut request = new HttpPut(url);
		HttpClient client = new DefaultHttpClient();
		request.addHeader("Accept", "text/plain");
		request.addHeader("Accept-Charset", "utf-8");

		try {
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs);
			entity.setContentEncoding(HTTP.UTF_8);
			request.setEntity(entity);
			HttpResponse response = client.execute(request);
			message = jacksonMapper.readValue(
					response.getEntity().getContent(),
					String.class);

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return message;
	}
}
